package com.sbs.gaoxinqutoilet.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.sbs.gaoxinqutoilet.R;


/**
 * 作者：author
 * 时间：2017/6/21:14:33
 * 邮箱：
 * 说明：
 */
public class ClearEditText extends AppCompatEditText implements View.OnTouchListener,View.OnFocusChangeListener,TextWatcher {

    private Drawable mClearIcon;
    private OnFocusChangeListener mOnFocusChangeListener;
    private OnTouchListener mOnTouchListener;


    public ClearEditText(Context context) {
        super(context);
        init(context);
    }


    public ClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public ClearEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }

    private void init(Context context) {
        final Drawable drawable= ContextCompat.getDrawable(context, R.drawable.icon_delete_32);
        final Drawable wrappedDrawable= DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(wrappedDrawable,getCurrentHintTextColor());
        mClearIcon=wrappedDrawable;
        mClearIcon.setBounds(0,0,mClearIcon.getIntrinsicHeight(),mClearIcon.getIntrinsicWidth());
        setClearnIconVisiable(false);
        super.setOnTouchListener(this);
        super.setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    private void setClearnIconVisiable(boolean visible) {
        mClearIcon.setVisible(visible,false);
        Drawable right = visible ? mClearIcon : null;
        final Drawable[] compoundDrawables=getCompoundDrawables();
        setCompoundDrawables(compoundDrawables[0],compoundDrawables[1]
        ,right,compoundDrawables[3]);

    }
    @Override
    public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
        mOnFocusChangeListener = onFocusChangeListener;
    }

    @Override
    public void setOnTouchListener(OnTouchListener onTouchListener) {
        mOnTouchListener = onTouchListener;
    }
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (isFocused()){
                setClearnIconVisiable(charSequence.length()>0);
            }
    }



    @Override
    public void afterTextChanged(Editable editable) {

    }

    @Override
    public void onFocusChange(View view, boolean hasfocus) {

        if (hasfocus){
            setClearnIconVisiable(getText().length()>0);
        }else {
            setClearnIconVisiable(false);
        }
        if (mOnFocusChangeListener!=null){
            mOnFocusChangeListener.onFocusChange(view,hasfocus);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

        final int x= (int) motionEvent.getX();
        if (mClearIcon.isVisible() && x > getWidth()  -getPaddingRight()-mClearIcon.getIntrinsicWidth()){
            if (motionEvent.getAction() ==MotionEvent.ACTION_UP){
                setError(null);
                setText("");
            }
            return true;
        }
        return mOnTouchListener!=null && mOnTouchListener.onTouch(view,motionEvent);
    }
}
